package com.yw.pms.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.yw.pms.dao.ProductDao;
import com.yw.pms.dao.common.Page;
import com.yw.pms.dao.entity.Product;
import com.yw.pms.utils.CommonResp;

@Service
public class ProductService {

	private final static Logger logger = LoggerFactory.getLogger(ProductService.class);

	@Resource
	ProductDao productDao;

	public CommonResp<Boolean> addProduct(Product product) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			productDao.insert(product);
			commonResp.success();
			logger.info("操作员编号【{}】新增产品【{}】成功", product.getOperatorId(), product);
		} catch (Exception e) {
			logger.error("新增产品发生异常", e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<List<Product>> queryProductList(Product product) {
		product.setPage(new Page(product.getPageNo()));
		CommonResp<List<Product>> commonResp = new CommonResp<List<Product>>();
		try {
			List<Product> listPage = productDao.listPage(product);
			commonResp.success();
			commonResp.setData(listPage);
			Map<String,Object> extraData = Maps.newHashMapWithExpectedSize(1);
			extraData.put("page", product.getPage());
			commonResp.setExtraData(extraData);
			logger.info("操作员ID【{}】查询产品信息【{}】成功",product.getOperatorId(),commonResp);
		} catch (Exception e) {
			logger.error("分页查询产品列表发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<List<Product>> getAllProduct(Product product) {
		CommonResp<List<Product>> commonResp = new CommonResp<List<Product>>();
		try {
			List<Product> listPage = productDao.listPage(product);
			commonResp.success();
			commonResp.setData(listPage);
			logger.info("查询所有产品信息【{}】成功",commonResp);
		} catch (Exception e) {
			logger.error("查询所有产品信息发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> deleteProduct(String productId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			Integer id = Integer.valueOf(productId);
			productDao.delete(id);
			commonResp.success();
			logger.info("删除产品【{}】成功",productId);
		} catch (Exception e) {
			logger.error("删除产品发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Product> get(String productId){
		CommonResp<Product> commonResp = new CommonResp<Product>();
		try {
			Product product = productDao.getById(Integer.valueOf(productId));
			commonResp.success();
			commonResp.setData(product);
			logger.info("查询产品【{}】成功",productId);
		} catch (Exception e) {
			logger.error("查询产品信息系统异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> isProductNameUniq(String productName, String productId) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			Product product = new Product();
			product.setProductName(productName);
			Product product2 = productDao.get(product);
			if(product2 == null) {
				commonResp.success();
			} else {
				commonResp.fail();
			}
			logger.info("校验产品名称【{}】重复结果【{}】（成功为不重复，失败为重复）",productName,commonResp.getMsg());
			return commonResp;
		} catch (Exception e) {
			logger.error("校验产品名称发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> updateProduct(Product product) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			productDao.update(product);
			commonResp.success();
			logger.info("更新产品【{}】成功",product.getProductName());
		} catch (Exception e) {
			logger.error("更新产品发生异常",e);
			commonResp.exception();
		}
		return commonResp;
	}
}
